#include "widget.h"
#include "ui_widget.h"

#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
#include <QDateTime>  // 生成随机数用
#include <QDebug>
#include <database.h>
#include "login.h"
#include <QSqlQuery>
#include <QRegExp> // 正则表达式
#include "showwidget.h"
#include <QCryptographicHash>
#include <QSqlError>
#include "userinfo.h"
#include "network.h"


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QLabel *l1 = new QLabel(this);
    l1->setPixmap(QPixmap(":/images/paidaxing.png"));
    l1->setScaledContents(true); //  缩放内容适应大小
    l1->setAlignment(Qt::AlignCenter);
//    l1->setGeometry(0,0,width(),height()); // 使得图片铺满整个页面

    // 将背景至于最底层
    l1->lower();

    // 登录
    Database* db = new Database();
    Network* network = new Network;

    db->createDBCon();
    this->sw = new ShowWidget;

    // 登录按钮
    connect(ui->loginBtn,&QPushButton::clicked,[=](){
        //1、获取账户和密码
        QString username = ui->uNameEdit->text();
        QString pwd = ui->pwdEdit->text();
        //2、判断账号是否存在
        if(!db->isAccExist(username))
        {
            qDebug() << "用户不存在";
            this->sw->writeFile("用户不存在");
            return;
        }
        //3、校验密码是否正确
        if(!db->isPwdExist(username,pwd))
        {
            qDebug()<<"密码错误";
            this->sw->writeFile("密码错误");
            return;
        }
        // 验证码验证
//        if (ui->verifyEdit->text() != ui->pushButton->text())
//        {
//            QMessageBox::warning(this,"错误","验证码不对！");
//            return ;
//        }
        //4、登录

        qDebug()<<"登录成功";
        this->sw->readFile();

        this->login_name = ui->uNameEdit->text(); // 获取登录者 用户名
        this->sw->writeFile(this->login_name,"登陆成功！");
        Database *db = new Database;
        db->setLoginName(login_name);
        this->sw = new ShowWidget;
        this->sw->show();

        Login* login = new Login();
        login->show();

        this->hide();
    });

    // 注册模块
    connect(ui->initBtn,&QPushButton::clicked,[=](){
        QWidget *w1 = new QWidget();
        w1->setFixedSize(500,300);
        w1->setWindowTitle("注册");
        // 背景图片
        QLabel *background = new QLabel(w1);
        background->setPixmap(QPixmap(":/images/touxiang.png"));
        background->setScaledContents(true); //  缩放内容适应大小
        background->setAlignment(Qt::AlignCenter);

        // 将背景至于最底层
        l1->lower();

        QLabel *l1 = new QLabel(w1);
        l1->setText("用户名：");
        l1->move(90,50);

        QLineEdit *edit1 = new QLineEdit(w1);
        edit1->move(160,50);
        edit1->setPlaceholderText("3-12位字符");

        QLabel *l2 = new QLabel(w1);
        l2->setText("密码：");
        l2->move(90,85);

        QLineEdit *edit2 = new QLineEdit(w1);
        edit2->move(160,85);
        edit2->setPlaceholderText("3-12位数字或者字符");
        edit2->setEchoMode(QLineEdit::Password);

        QLabel *l3 = new QLabel(w1);
        l3->setText("确认密码：");
        l3->move(90,120);

        QLineEdit *edit3 = new QLineEdit(w1);
        edit3->move(160,120);
        edit3->setPlaceholderText("确认密码！");
        edit3->setEchoMode(QLineEdit::Password);

        QPushButton *btn1 = new QPushButton(w1);
        btn1->setText("确定");
        btn1->resize(70,27);
        btn1->move(160,160);

        QPushButton *btn2 = new QPushButton(w1);
        btn2->setText("取消");
        btn2->resize(70,27);
        btn2->move(260,160);

        this->hide();
        w1->show();
        // 正确设置默认按钮
        ui->pushButton->setAutoDefault(true);   // 允许成为默认按钮
        ui->pushButton->setDefault(true);       // 设置为当前默认按钮

        // 禁用其他按钮的默认属性
        ui->initBtn->setAutoDefault(false);
        connect(btn1,&QPushButton::clicked,[=](){
            if (edit2->text() != edit3->text())
            {
                QMessageBox::warning(w1,"错误","密码不一致！");
                this->sw->writeFile("用户注册，两次密码输入不一致！");
                return ;
            }
            QString username = edit1->text().trimmed();
            QString pwd = edit2->text().trimmed();
            QRegExp r1("[a-zA-Z]{3,12}");
            QRegExp r2("[a-zA-Z0-9]{3,12}");
            if (r1.indexIn(username) == -1)
            {
                QMessageBox::information(w1,"错误","用户名格式不对");
                this->sw->writeFile("用户注册，用户名格式不对");

                return ;
            }
            if (r2.indexIn(pwd) == -1)
            {
                QMessageBox::information(w1,"错误","密码格式不对");

                this->sw->writeFile("用户注册，密码格式不对");
                return ;
            }
            //1、对密码进行md5加密处理
            QString pwdMD5;
            QByteArray str;
            str = QCryptographicHash::hash(pwd.toLatin1(), QCryptographicHash::Md5); // 进行MD5加密
            pwdMD5.append(str.toHex()); // 将加密结果转换为十六进制字符串

            UserInfo* user = new UserInfo();
            user->setUserName(username);
            user->setPassword(pwdMD5);

            int code = db->addUser(user);
            qDebug() << code;
            if(code==101)
            {
                //用户已存在
                 QMessageBox::warning(w1,"错误","用户已存在");
                 this->sw->writeFile("用户注册，用户名已存在");
                 return ;
            }
            else if(code ==200)
            {
                this->login_name = edit1->text();

                 QMessageBox::information(w1,"成功","注册成功");
                 this->sw->writeFile(this->login_name,"用户注册成功");
                 w1->hide();
                 this->show();
                 return ;
            }else if(code==404)
            {
                 QMessageBox::warning(w1,"错误","插入失败");
                 return ;
            }

        });

        // 取消
        connect(btn2,&QPushButton::clicked,[=](){
            w1->hide();
            this->show();
        });
    });
}

Widget::~Widget()
{
    delete ui;
}



void Widget::on_eyeBtn_clicked()
{
    this->isPwd = !this->isPwd;
    if (this->isPwd)
    {
        ui->pwdEdit->setEchoMode(QLineEdit::Password);
    }
    else {
        ui->pwdEdit->setEchoMode(QLineEdit::Normal);
    }
}


void Widget::on_pushButton_clicked()
{
    qsrand(QDateTime::currentMSecsSinceEpoch());
    int a = qrand() % 10000;
    ui->pushButton->setText(QString::number(a));
}

QString Widget::getLoginName()
{
    return this->login_name;
}
